<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del"></el-button>
				<el-button type="primary" plain :disabled="selection.length == 0" @click="set_role">分配角色</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="岗位名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" @cell-dblclick="cellClick">
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="岗位编码" prop="code"></el-table-column>
				<el-table-column label="岗位名称" prop="name"></el-table-column>
				<el-table-column label="所属角色" prop="role_name" sortable='custom'>
					<template #default="scope">
						<div class="input-box">
							<el-select v-model="scope.row.role_id" v-if="scope.row.flag" multiple filterable style="width: 100%" :ref="`role_id` + scope.row.id">
								<el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
							<span v-if="!scope.row.flag">{{ scope.row.role_name }}</span>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="排序" prop="sort"></el-table-column>
				<el-table-column label="备注" prop="remark">
					<template #default="scope">
						<div class="input-box">
							<el-input size="mini" v-model="scope.row.remark" v-if="scope.row.flag" :ref="`remark` + scope.row.id"></el-input>
							<span v-if="!scope.row.flag">{{ scope.row.remark }}</span>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="状态" prop="status">
					<template #default="scope">
						<el-tag v-if="scope.row.status == 1" type="success">启用</el-tag>
						<el-tag v-if="scope.row.status == 0" type="danger">停用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="create_time"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button type="primary" v-if="scope.row.flag" size="small" @click="cell_save(scope.row)">保存</el-button>
							<el-button type="danger" v-if="scope.row.flag" size="small" @click="cell_save(scope.row)">取消</el-button>

							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save = false"></save-dialog>
	<save-role v-if="dialog_role.save" ref="saveRole" @success="handleSaveSuccess" @closed="dialog_role.save = false"></save-role>
</template>

<script>
import saveDialog from './save'
import saveRole from './set_role'
export default {
	name: '岗位管理',
	components: {
		saveDialog,
		saveRole,
	},
	data() {
		return {
			dialog: {
				save: false
			},
			dialog_role: {
				save: false
			},
			apiObj: this.$API.system.jobs.lists,
			selection: [],
			search: {
				keyword: null
			},
			roles: [],
		}
	},
	mounted() {
		this.getGroup()
	},
	directives: {
		// 注册一个局部的自定义指令 v-focus  失去焦点指令
		focus: {
			// 指令的定义
			inserted: function (el) {
				console.log(el, 'el')
				// 聚焦元素
				el.querySelector('input').focus()
			}
		}
	},
	methods: {
		//双击单元格后，显示input，并通过v-focus让input自动获取焦点
		cellClick(row) {
			row.flag = true
			var that = this
			setTimeout(function () {
				that.$nextTick(() => {
					that.$refs['remark' + row.id].focus()
				})
			}, 100)
		},
		//input框失去焦点事件
		async cell_save(row) {
			row.flag = false
			//自行请求编辑
		},
		//加载树数据
		async getGroup() {
			var res = await this.$API.system.role.list.get();
			this.roles = res.data.rows;
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		set_role() {
			var admin_ids = [];
			this.selection.forEach((item) => {
				admin_ids.push(item.id)
			})
			this.dialog_role.save = true
			this.$nextTick(() => {
				this.$refs.saveRole.open("set").setData({ ids: admin_ids })
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row) {
			var reqData = { id: row.id }
			var res = await this.$API.system.jobs.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//批量删除
		async table_del_all(delete_ids) {
			var reqData = { ids: delete_ids }
			var res = await this.$API.system.jobs.deleteAll.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})

			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
		}
	}
}
</script>

<style></style>
